package edu.faytechcc.csc.chat;

import edu.faytechcc.csc.database.LogIntoDatabase;
import edu.faytechcc.csc.gui.ChatPanel;
import edu.faytechcc.csc.gui.ChatWindow;
import edu.faytechcc.csc.gui.ServerFrame;
import edu.faytechcc.csc.messages.Connection;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 * Please describe what this class does here.
 *
 * CSC 251 0001 DATE:
 *
 * @author Stephanie Mckenzie
 */
public class MultiChatServer
{

    public static void main(String[] args)
    {
        ServerFrame frame = new ServerFrame();

        ServerSocket serverSocket = null;

        ArrayList<Connection> connections = new ArrayList<>();

        try
        {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e)
        {
            System.out.println("Could not listen on port 4444");
            System.exit(1);
        }

        Socket clientSocket = null;
        try
        {
            boolean connect = true;
            while (connect)
            {
                System.out.println("Waiting for client connection...");
                clientSocket = serverSocket.accept();
                System.out.println("Client connected: "
                        + clientSocket.getInetAddress());
                System.out.println("+++++++++++++\n");

                Connection conn = new Connection(clientSocket, "Client");
                ChatWindow window = new ChatWindow(conn, "Server");
                conn.addChatWindow(window);
                window.createAndShowGUI();
                window.enableTextField();
                conn.connect();
                connections.add(conn);

            }
            serverSocket.close();

        } catch (IOException e)
        {
            JOptionPane.showMessageDialog(null, "Client Accept failded");
        }
    }

    public String logIn(String name, char[] password)
    {
        LogIntoDatabase logIn = new LogIntoDatabase(name, password);

        return logIn.toString();
    }
}
